package com.github.lee.core.common

import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Color
import android.net.ConnectivityManager
import androidx.annotation.ColorInt
import java.io.File
import java.security.MessageDigest
import java.util.*


/**生成随机颜色*/
@ColorInt
fun generateBeautifulColor(): Int {
    val random = Random()
    val r = random.nextInt(150) + 50
    val g = random.nextInt(150) + 50
    val b = random.nextInt(150) + 50
    return Color.rgb(r, g, b)
}

/**
 * 获取当前应用程序的版本号
 */
fun getVersionCode(context: Context): Int {
    try {
        // 包的管理者，获取应用程序清单文件中的所有信息
        val packageManager = context.packageManager // 获取包的管理者
        val packageInfo = packageManager.getPackageInfo(
            context.packageName, 0
        )
        @Suppress("DEPRECATION")
        return packageInfo.versionCode // 获取应用程序的版本号
    } catch (e: PackageManager.NameNotFoundException) {
        // 找不到包名的异常
    }
    return 0
}

/**
 * 获取当前应用程序的版本号
 */
fun getVersionName(context: Context): String {
    try {
        // 包的管理者，获取应用程序清单文件中的所有信息
        val packageManager = context.packageManager // 获取包的管理者
        val packageInfo = packageManager.getPackageInfo(
            context.packageName, 0
        )
        return packageInfo.versionName // 获取应用程序的版本号
    } catch (e: PackageManager.NameNotFoundException) {
        // 找不到包名的异常
    }
    return ""
}

/**
 * 获取渠道号
 *
 * @param context 上下文对象
 */
fun <T> getMetaData(context: Context, key: String, defaultValue: T? = null): T? {
    try {
        val pm = context.packageManager
        val appInfo = pm.getApplicationInfo(
            context.packageName, PackageManager.GET_META_DATA
        )
        val get = appInfo.metaData.get(key) ?: return defaultValue
        @Suppress("UNCHECKED_CAST")
        return get as T
    } catch (ignored: PackageManager.NameNotFoundException) {
        ignored.printStackTrace()
    }
    return defaultValue
}


//fun getCurrentNetwork(context: Context): NetworkListenerReceiver.NetworkState {
//    //获得ConnectivityManager对象
//    val cm: ConnectivityManager =
//        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
//    //获取WIFI连接的信息
//    val wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
//    //移动数据网络
//    val dataNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
//
//    return if (wifiNetworkInfo?.isConnected == true && dataNetworkInfo?.isConnected == true) {
//        //wifi和移动数据都连接
//        NetworkListenerReceiver.NetworkState.ALL
//    } else if (wifiNetworkInfo?.isConnected == true && dataNetworkInfo?.isConnected != true) {
//        //wifi链接  移动数据未连接
//        NetworkListenerReceiver.NetworkState.WIFI
//    } else if (wifiNetworkInfo?.isConnected != true && dataNetworkInfo?.isConnected == true) {
//        //WIFI已断开,移动数据已连接
//        NetworkListenerReceiver.NetworkState.MOBILE
//    } else if (wifiNetworkInfo?.isConnected != true && dataNetworkInfo?.isConnected != true) {
//        //网络未连接
//        NetworkListenerReceiver.NetworkState.NONE
//    } else {
//        NetworkListenerReceiver.NetworkState.UN_KNOW
//    }
//}


fun md5(file: File): String {
    if (!file.exists() || !file.isFile) {
        throw IllegalAccessException("The file '${file.absolutePath}' does not exists or it is not a file")
    }
    return md5(file.readBytes())
}

fun md5(input: String): String {
    return md5(input.toByteArray())
}

fun md5(input: ByteArray): String {
    try {
        val digest = MessageDigest.getInstance("MD5")
        digest.update(input)
        val sb = StringBuilder()
        for (item in digest.digest()) {
            //获取低八位有效值
            val i = item.toInt() and 0xff
            //将整数转化为16进制
            var hexString = Integer.toHexString(i)
            if (hexString.length < 2) {
                //如果是一位的话，补0
                hexString = "0$hexString"
            }
            sb.append(hexString)
        }
        return sb.toString()
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return ""
}


fun dp2px(context: Context, dipValue: Float): Float {
    val scale = context.resources?.displayMetrics!!.density
    return (dipValue * scale + 0.5f)
}

fun getScreenWidth(context: Context): Int {
    val displayMetrics = context.resources!!.displayMetrics
    return displayMetrics.widthPixels
}

fun getScreenHeight(context: Context): Int {
    val displayMetrics = context.resources!!.displayMetrics
    return displayMetrics.heightPixels
}

